<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class DynamicDataAttr extends Basic
{
    public $attr_name_repeat;
    
    public static function tableName()
    {
        return '{{%dynamic_data_attr}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['dynamic_id', 'required', 'message' => 'dynamic_id不能为空', 'on' => 'add'],
            ['dynamic_id', 'integer', 'message' => 'dynamic_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'dynamic_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'dynamic_id最大存储18446744073709551615'],
            ['attr_name', 'required', 'message' => 'attr_name不能为空', 'on' => 'add'],
            ['attr_name', 'string', 'message' => 'attr_name必须是字符串',
                                'min' => 2, 'tooShort' => 'attr_name最短2位',
                                'max' => 16, 'tooLong' => 'attr_name最长16位'],
            ['attr_enname', 'required', 'message' => 'attr_enname不能为空', 'on' => 'add'],
            ['attr_enname', 'string', 'message' => 'attr_enname必须是字符串',
                                'min' => 2, 'tooShort' => 'attr_enname最短2位',
                                'max' => 16, 'tooLong' => 'attr_enname最长16位'],
            ['attr_enname', 'validateEnname', 'on' => 'add'],
            // ['attr_type', 'default', 'value' => 1],
            ['attr_type', 'integer', 'message' => 'attr_type必须为数字，或数字字符串'],
            ['attr_type', 'in', 'range' => array(1, 3, 4, 5, 7, 8), 'message' => 'attr_type值必须在134578中选择'],
            // ['attr_value', 'default', 'value' => ''],
            ['attr_value', 'string', 'message' => 'attr_value必须是字符串'],
            ['can_null', 'integer', 'message' => 'can_null必须为数字，或数字字符串'],
            ['can_null', 'in', 'range' => array(1, 2), 'message' => 'can_null值必须在1,2中选择'],
            // ['default_val', 'default', 'value' => ''],
            ['default_val', 'string', 'message' => 'default_val必须是字符串',
                                'max' => 512, 'tooLong' => 'default_val最长512位'],
            // ['state', 'default', 'value' => 2],
            ['state', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['state', 'in', 'range' => array(1, 2), 'message' => 'state值必须在1,2中选择'],
        ];
    }

    /*rules 验证登录信息是否吻合*/
    public function validateEnname()
    {
        if(!$this->hasErrors()){
            $data = self::find()->where('app_id = :app_id and dynamic_id = :dynamic_id and attr_enname = :name', [':app_id' => $this->app_id, ':dynamic_id' => $this->dynamic_id, ':name' => $this->attr_enname])->asArray()->one();
            // P($data);
            if(!is_null($data)){
                $this->addError('attr_enname_repeat', '名称重复了');
            }
        }
    }


    /*添加*/
    public function addAttr($data, $app_id)
    {
        $this->scenario = 'add';
        if(isset($data['DynamicDataAttr']['can_null'])){
            $data['DynamicDataAttr']['can_null'] = Tool::changeBool($data['DynamicDataAttr']['can_null']);
        }
        $data['DynamicDataAttr']['app_id'] = $app_id;
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                return true;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyAttr($data, $id)
    {
        // P($data);
        $dynamicDataAttr = self::find()->where('attr_id = :id', [':id' => $id])->one();
        if(isset($data['DynamicDataAttr']['can_null'])){
            $data['DynamicDataAttr']['can_null'] = Tool::changeBool($data['DynamicDataAttr']['can_null']);
        }
        // P($dynamicDataAttr);
        if(!is_null($dynamicDataAttr)){
            if($this->load($data) and $this->validate()){
                if(isset($this->attr_name)){
                    $dynamicDataAttr->attr_name = $this->attr_name;
                }
                if(isset($this->attr_enname)){
                    $dynamicDataAttr->attr_enname = $this->attr_enname;
                }
                if(isset($this->attr_type)){
                    $dynamicDataAttr->attr_type = $this->attr_type;
                }
                if(isset($this->attr_value)){
                    $dynamicDataAttr->attr_value = $this->attr_value;
                }
                if(isset($this->can_null)){
                    $dynamicDataAttr->can_null = $this->can_null;
                }
                if(isset($this->default_val)){
                    $dynamicDataAttr->default_val = $this->default_val;
                }
                if($dynamicDataAttr->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    
}